<?php

namespace App\Console\Commands;

use App\Imports\MultipleImport;
use App\Models\GoodsShelf;
use App\Models\ModelNumber;
use App\Models\Product;
use App\Models\ProductModelNumber;
use App\Models\Specification;
use App\Rep\OrderRep;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AddSemiProduct extends Command
{
    /**
     * 添加半成品备货.
     * php artisan command:addsemiproduct
     *
     * @var string
     */
    protected $signature = 'command:addsemiproduct';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $file = public_path('excel').'/库存盘点.xlsx';//获取文件路径
        $import = new MultipleImport(3);
        Excel::import($import, $file);
        $results = $import->sheet[0]->data->toArray();
        unset($results[0]);
        $orderRep = new OrderRep();
        foreach($results as $k=>$v){
            $this->info('导入：产品名称='.$v[0].";型号=".$v[1].";规格=".$v[2].";库存数量=".$v[3].";仓库=".$v[4]);
            $product = Product::where('product_name',$v[0])->first();
            $model_number = ModelNumber::updateOrCreate(['model_number'=>$v[1]],['model_number'=>$v[1]]);
            ProductModelNumber::updateOrCreate(
                ['product_id'=>$product->id,'model_number_id'=>$model_number->id],
                ['product_id'=>$product->id,'model_number_id'=>$model_number->id]
            );
            if(!empty($v[2])){
                if($v[2] == '2*2.2'){
                    $v[2] = '2*2.2米';
                }elseif($v[2] == '1.8*2.2'){
                    $v[2] ='1.8*2.2米';
                }
                $specification = Specification::updateOrCreate(['specification'=>$v[2]],['specification'=>$v[2]]);
                $specification_id = $specification->id;
            }else{
                $specification_id = '';
            }
            $num = $v[3];
            $warehouse_id = 3;
            $goods_self = GoodsShelf::where('goods_shelf_name',$v[4])->first();
            $goods_self_id = $goods_self->id;
            $data = [
                'modelid'=>$product->modelid,
                'num'=>$num,
                "warehouse_id"=>$warehouse_id,
                "goods_shelf_id"=>$goods_self_id,
                "batch_number"=>'',
                "serial_number"=>$orderRep->automaticGenerationSerialNumberRep(),
                "order_number"=>$orderRep->automaticGenerationOrderNumberRep(),
                "order_date"=>date('Y-m-d H:i:s'),
                "delivery_date"=> date('Y-m-d H:i:s'),
                "account_executive_id"=>'',
                "product_id"=>$product->id,
                "model_number_id"=>$model_number->id,
                "color_id"=> null,
                "size"=> "",
                "is_chuangtougui"=> 0,
                "specification_id"=> $specification_id,
                "direction"=> "",
                "customer_id"=> 0,
                "received"=> "",
                "emergency_procedure"=> "1",
                "is_replenishment"=> "1",
                "is_stock_up"=> "1",
                "receivables"=> 0,
                "remark"=> "半成品盘点批量导入",
                "id"=> "",
                "is_kucun"=> "2",
                "unit_price"=> "",
                "discount"=> "",
                "spare_money"=> 0,
                "customer_name"=> "",
                "account_executive"=> "",
                "model_number"=> $v[1],
                "pid"=> 2,
                "semi_product"=> []
            ];
            $params['order_data'][] = $data;
            $params['user_id'] = 1;

        }
        $res = $orderRep->createOrderRep($params);
        $this->info('success！！！'.json_encode($res));
    }
}
